package com.ljyanimo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljyanimo.entity.SysMenu;
import com.ljyanimo.entity.SysMenuRole;
import com.ljyanimo.mapper.SysMenuMapper;
import com.ljyanimo.mapper.SysMenuRoleMapper;
import com.ljyanimo.response.ServerResponse;
import com.ljyanimo.service.ISysMenuRoleService;
import com.ljyanimo.service.ISysMenuService;
import com.ljyanimo.vo.NavigationMenuVo;
import com.ljyanimo.vo.TreeEditVo;
import com.ljyanimo.vo.TreeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2020-10-16
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private ISysMenuRoleService iSysMenuRoleService;

    @Resource
    private SysMenuRoleMapper sysMenuRoleMapper;

    @Override
    public List<NavigationMenuVo> selectMenuByUserId(Integer userId) {
        List<Integer> ids = iSysMenuRoleService.selectMenuRoleByUserId(userId);
        return baseMapper.selectMenuByIds(ids);
    }


    @Override
    public List<SysMenu> selectListMenu() {
        return baseMapper.selectList(new QueryWrapper<SysMenu>().orderByAsc("menu_weight"));
    }

    @Override
    public List<TreeVo> tree() {
        return baseMapper.tree();
    }

    @Override
    public List<TreeEditVo> treeEdit(Integer id) {
        List<TreeEditVo> list;
        List<SysMenuRole> sysMenuRoles = sysMenuRoleMapper.selectList(new QueryWrapper<SysMenuRole>().eq("role_id", id));
        List<SysMenu> menuList = baseMapper.selectList(null);
        list = menuList.stream().map(e->{
            TreeEditVo vo = new TreeEditVo();
            vo.setId(e.getId());
            vo.setPid(e.getParentId());
            vo.setName(e.getMenuName());
            for(SysMenuRole sysMenuRole : sysMenuRoles){
                if(e.getId().equals(sysMenuRole.getMenuId())){
                    vo.setChecked(Boolean.TRUE);
                }
            }
            return vo;
        }).collect(Collectors.toList());
        return list;
    }
}
